package com.qs.controller;

import com.qs.commen.Response;
import com.qs.pojo.SysDept;
import com.qss.service.DeptService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/dept")
public class DeptController {
    @Reference
    private DeptService service;

    @GetMapping("/todata")
    public String data(){
        return "dept/data";
    }
    /**
     * 异步加载分页数据
     * @param dept

     * @return
     */
    @PostMapping("/data")
    @ResponseBody
    public Response data(SysDept dept) {

        List<SysDept> depts=service.queryDepts(dept);
        return new Response(200,depts);
    }

    /**
     * 跳转到编辑或新增页面
     * @param dept
     * @param model
     * @return
     */
    @GetMapping("/toEdit")
    public String toEdit(SysDept dept, Model model){

        if(dept!=null && dept.getDeptId()!=null){
            dept = service.queryUserById(dept.getDeptId());
            model.addAttribute("dept",dept);
        }
        return "dept/edit";
    }
    /**
     * 新增或更新用户信息
     * @param dept
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public Response save(SysDept dept){

        boolean status = service.saveUser(dept);
        if(status){
            return Response.success("保存成功");
        }
        return new Response(400,"操作失败");
    }

    @DeleteMapping("/deletes")
    @ResponseBody
    public Response delete(Integer[] ids){
        boolean status = service.delUserById(ids);
        if(status) return Response.success("删除成功！");
        return Response.error();
    }
    @PostMapping("checkDeptName")
    @ResponseBody
    public  boolean check(SysDept dept){
      SysDept dept1= service.queryDeptByName(dept.getDeptName());
        if (dept1==null)return true;
        return false;
    }
}
